package com.pureut.quality.controller;

import com.pureut.common.core.web.controller.BaseController;
import com.pureut.common.core.web.domain.AjaxResult;
import com.pureut.common.core.web.page.TableDataInfo;
import com.pureut.common.log.annotation.Log;
import com.pureut.common.log.enums.BusinessType;
import com.pureut.common.security.annotation.RequiresPermissions;
import com.pureut.quality.domain.dto.QualityInspectionDocumentDto;
import com.pureut.quality.domain.vo.*;
import com.pureut.quality.service.QualityInspectionDocumentService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/***
 * 检验单据控制层
 * Author:C
 * Date:2023/02/06 10:30
 * @DESC
 */
@RestController
@RequestMapping("/quality-inspection-document")
public class QualityInspectionDocumentController extends BaseController {

    @Resource
    QualityInspectionDocumentService qualityInspectionDocumentService;


    /**
     * 检验单据列表
     */
    @RequiresPermissions("quality:inspecmanage:testbill:list")
    @GetMapping("/list")
    public TableDataInfo getList(QualityInspectionDocumentVo qualityInspectionDocumentVo) {
        startPage();
        //查询检验单据列表
        List<QualityInspectionDocumentDto> list = qualityInspectionDocumentService.getDocumentList(qualityInspectionDocumentVo);
        return getDataTable(list);
    }

    /**
     * 检验单据新增
     */
    @PostMapping("/add")
    @Log(title = "检验单据新增", businessType = BusinessType.INSERT)
    public AjaxResult add(@Validated @RequestBody QualityInspectionDocumentVo qualityInspectionDocumentVo) throws Exception {
        return toAjax(qualityInspectionDocumentService.add(qualityInspectionDocumentVo));
    }

    /**
     * 检验单据新增(脱脂送检使用)
     */
    @PostMapping("/addDegreas")
    @Log(title = "检验单据新增", businessType = BusinessType.INSERT)
    public boolean addDegreas(@RequestBody QualityInspectionDocumentVo qualityInspectionDocumentVo) throws Exception {
        return qualityInspectionDocumentService.addDegreas(qualityInspectionDocumentVo);
    }

    /**
     * 检验单据新增(仓库模块使用)
     */
    @PostMapping("/addWares")
    @Log(title = "检验单据新增", businessType = BusinessType.INSERT)
    public boolean addWares(@RequestBody QualityInspectionDocumentVo qualityInspectionDocumentVo) throws Exception {
        return qualityInspectionDocumentService.addWares(qualityInspectionDocumentVo);
    }

    /**
     * 查看单据信息
     */
    @RequiresPermissions("quality:inspecmanage:testbill:query")
    @GetMapping("/view/{id}")
    public AjaxResult add(@PathVariable("id") Long id) {
        return AjaxResult.success(qualityInspectionDocumentService.getView(id));
    }


    /**
     * 样品检验(检验项目列表)
     */
    @RequiresPermissions("quality:inspecmanage:testbill:mark")
    @GetMapping("/sampleInspection")
    public AjaxResult sampleInspection(QualitySchemeConfigurationVo qualitySchemeConfigurationVo) {
        return AjaxResult.success(qualityInspectionDocumentService.sampleInspection(qualitySchemeConfigurationVo));
    }

    /**
     * 来料处理
     */
    @RequiresPermissions("quality:inspecmanage:testbill:processing")
    @PostMapping("/processingIncomingMaterials")
    @Log(title = "来料处理", businessType = BusinessType.UPDATE)
    public AjaxResult processingIncomingMaterials(@RequestBody ProcessingIncomingMaterialsVo processingIncomingMaterialsVo) {
        return toAjax(qualityInspectionDocumentService.processingIncomingMaterials(processingIncomingMaterialsVo));
    }

    /**
     * 品质处理
     */
    @RequiresPermissions("quality:inspecmanage:testbill:quality")
    @PostMapping("/qualityTreatment")
    @Log(title = "品质处理", businessType = BusinessType.UPDATE)
    public AjaxResult qualityTreatment(@RequestBody ProcessingIncomingMaterialsVo processingIncomingMaterialsVo) throws Exception {
        return toAjax(qualityInspectionDocumentService.qualityTreatment(processingIncomingMaterialsVo));
    }

    /**
     * 批量审核
     */
    @PostMapping("/batchAudit")
    @RequiresPermissions("quality:inspecmanage:testbill:batchAudit")
    @Log(title = "批量审核", businessType = BusinessType.UPDATE)
    public AjaxResult batchAudit(@RequestBody OrderBatchReviewVo orderBatchReviewVo) throws Exception {
        return toAjax(qualityInspectionDocumentService.batchAudit(orderBatchReviewVo));
    }


    /**
     * 删除品质检验单Feign
     */
    @Log(title = "删除品质检验单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{relevanceDoc}")
    public AjaxResult removeDoc(@PathVariable("relevanceDoc") String relevanceDoc) {
        return toAjax(qualityInspectionDocumentService.removeDoc(relevanceDoc));
    }

    /**
     * 生产看板-班组近7天良品率(feign)
     */
    @GetMapping("/getYield")
    public AjaxResult getYield(String lineCode) {
        return AjaxResult.success(qualityInspectionDocumentService.getYield(lineCode));
    }

    /**
     * 根据批次号查询品质检验单(fegin调用)
     */
    @GetMapping("/getDocumentByBatchNumber")
    public AjaxResult getDocumentByBatchNumber(String batchNumber, String degreasDocumentNumber) {
        return AjaxResult.success(qualityInspectionDocumentService.getDocumentByBatchNumber(batchNumber, degreasDocumentNumber));
    }

    /**
     * 生产看板-本月品质不良分布
     */
    @GetMapping("/getBadMonth")
    public AjaxResult getBadMonth(String lineCode) {
        return AjaxResult.success(qualityInspectionDocumentService.getBadMonth(lineCode));
    }

    /**
     * 修改品质检验单数据状态（fegin调用）
     */
    @PostMapping("/updateQualityInspectionDocument")
    public AjaxResult updateQualityInspectionDocument(@RequestBody UpdateQualityStatusVo updateQualityStatusVo) {
        return toAjax(qualityInspectionDocumentService.updateQualityInspectionDocument(updateQualityStatusVo));
    }

//    /**
//     * 导出单据
//     */
//    @PostMapping("/qualityInspectionDocumentExport")
//    public void qualityInspectionDocumentExport(HttpServletResponse response, ProductionPickingExportVo productionPickingExportVo) throws Exception {
//        qualityInspectionDocumentService.qualityInspectionDocumentExport(response,productionPickingExportVo);
//    }

}
